home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / lib / tex / dayofweek.tex < prev    next >
Encoding:
Text File  |  1988-04-18  |  5.5 KB  |  150 lines

  1. %
  2. %    \DayOfWeek    expands to the day of the week ("Sunday", etc.)
  3. %    \PhaseOfMoon    expands to the phase of the moon
  4. %
  5. %    Written by Martin Minow of DEC (minow%bolt.dec@decwrl.dec.com).
  6. %
  7. \def\DayOfWeek{%
  8. %
  9. %     Calculate day of the week, return "Sunday", etc.
  10. %
  11.   \newcount\dow                % Gets day of the week
  12.   \newcount\leap            % Leap year fingaler
  13.   \newcount\x                % Temp register
  14.   \newcount\y                 % Another temp register
  15. %        leap = year + (month - 14)/12;
  16.   \leap=\month \advance\leap by -14 \divide\leap by 12
  17.   \advance\leap by \year
  18. %        dow = (13 * (month + 10 - (month + 10)/13*12) - 1)/5
  19.   \dow=\month \advance\dow by 10
  20.   \y=\dow \divide\y by 13 \multiply\y by 12
  21.   \advance\dow by -\y \multiply\dow by 13 \advance\dow by -1 \divide\dow by 5
  22. %        dow += day + 77 + 5 * (leap % 100)/4
  23.   \advance\dow by \day \advance\dow by 77
  24.   \x=\leap \y=\x \divide\y by 100 \multiply\y by 100 \advance\x by -\y
  25.   \multiply\x by 5 \divide\x by 4 \advance\dow by \x
  26. %        dow += leap / 400
  27.   \x=\leap \divide\x by 400 \advance\dow by \x
  28. %        dow -= leap / 100 * 2;
  29. %        dow = (dow % 7)
  30.   \x=\leap \divide\x by 100 \multiply\x by 2 \advance\dow by -\x
  31.   \x=\dow \divide\x by 7 \multiply\x by 7 \advance\dow by -\x
  32.   \ifcase\dow Sunday\or Monday\or Tuesday\or Wednesday\or
  33.     Thursday\or Friday\or Saturday\fi
  34. }
  35. %%
  36. %%
  37. %%
  38. \def\PhaseOfMoon{%    Calculate the phase of the (civil) moon.
  39. %
  40. % The routine calculates the year's epact (the age of the moon on Jan 1.),
  41. % adds this to the number of days in the year, and calculates the phase
  42. % of the moon for this date.  It returns the phase as a string, e.g.,
  43. % "new", "full", etc.
  44. %
  45. % In the algorithm:
  46. %
  47. %    diy    Is the day of the year - 1 (i.e., Jan 1 is day 0).
  48. %
  49. %    golden    Is the number of the year in the Mentonic cycle, used to
  50. %        determine the position of the calender moon.
  51. %
  52. %    epact    Is the age of the calender moon (in days) at the beginning
  53. %        of the year.  To calculate epact, two century-based
  54. %        corrections are applied:
  55. %        Gregorian:    (3 * cent)/4 - 12
  56. %            is the number of years such as 1700, 1800 when
  57. %            leap year was not held.
  58. %        Clavian:    (((8 * cent) + 5) / 25) - 5
  59. %            is a correction to the Mentonic cycle of about
  60. %            8 days every 2500 years.  Note that this will
  61. %            overflow 16 bits in the year 409600.  Beware.
  62. %
  63. % The algorithm is accurate for the Gregorian calender only.
  64. %    
  65. % The magic numbers used in the phase calculation are as follows:
  66. %     29.5        The moon's period in days.
  67. %    177        29.5 scaled by 6
  68. %     22        (29.5 / 8) scaled by 6 (this gets the phase)
  69. %     11        ((29.5 / 8) / 2) scaled by 6
  70. %
  71. % Theoretically, this should yield a number in the range 0 .. 7.  However,
  72. % two days per year, things don't work out too well.
  73. %
  74. % Epact is calculated by the algorithm given in Knuth vol. 1 (calculation
  75. % of Easter).  See also the article on Calenders in the Encyclopaedia
  76. % Britannica and Knuth's algorithm in CACM April 1962, page 209.
  77. %
  78.   \newcount\cent        % Century number (1979 == 20)
  79.   \newcount\epact        % Age of the moon on Jan. 1
  80.   \newcount\diy            % Day in the year
  81.   \newcount\golden        % Moon's golden number
  82.   \newcount\x            % Temp
  83.   \newcount\m            % Temp for modulus
  84.   \diy=\day \advance\diy by \ifcase\month        % Jan 1 == 0
  85.     -1\or -1\or 30\or 58\or 89\or 119\or 150\or    % Jan .. Jun
  86.     180\or 211\or 241\or 272\or 303\or 333\fi    % Jul .. Dec
  87. %        if ((month > 2) && ((year % 4 == 0) && 
  88. %            ((year % 400 == 0) || (year % 100 != 0))))
  89. %            diy++;        /* Leapyear fixup    */
  90.   \ifnum \month>2
  91.     \x=\year \m=\x \divide\m by 4 \multiply\m by 4 \advance\x by -\m
  92.     \ifnum \x=0                % month > 2 and maybe leapyear
  93.       \x=\year \m=\x \divide\m by 400 \multiply\m by 400 \advance\x by -\m
  94.       \ifnum \x=0            % 2000 is a leap year
  95.     \advance\diy by 1        % so it's one day later
  96.       \else                % not 2000, check other '00's
  97.     \x=\year \m=\x \divide\m by 100 \multiply\m by 100 \advance\x by -\m
  98.     \ifnum \x>0            % not some other '00' year
  99.         \advance\diy by 1        % it's still one day later
  100.     \fi                % not odd century
  101.       \fi                % not 2000-type century
  102.     \fi                    % not leapish year
  103.   \fi                    % not march or later
  104. %        cent = (year / 100) + 1;    /* Century number    */
  105. %        golden = (year % 19) + 1;    /* Golden number    */
  106.   \cent=\year \divide\cent by 100 \advance\cent by 1
  107.   \golden=\year
  108.   \m=\year \divide\m by 19 \multiply\m by 19 \advance\golden by -\m
  109.   \advance\golden by 1
  110. %        epact = ((11 * golden) + 20    /* Golden number    */
  111. %        + (((8 * cent) + 5) / 25) - 5    /* 400 year cycle    */
  112. %        - (((3 * cent) / 4) - 12)) % 30;/* Leap year correction    */
  113.   \epact=11 \multiply\epact by \golden
  114.   \advance\epact by 20
  115.   \x=8 \multiply\x by \cent \advance\x by 5
  116.   \divide\x by 25 \advance\x by -5
  117.   \advance\epact by \x
  118.   \x=3 \multiply\x by \cent \divide\x by 4 \advance\x by -12
  119.   \advance\epact by -\x
  120.   \m=\epact \divide\m by 30 \multiply\m by 30 \advance\epact by -\m
  121. %    if (epact <= 0)
  122. %        epact += 30;            /* Age range is 1 .. 30    */
  123. %    if ((epact == 25 && golden > 11) || epact == 24)
  124. %        epact++;
  125.   \ifnum \epact<0
  126.     \advance\epact by 30
  127.   \fi
  128.   \ifnum \epact=25
  129.     \ifnum \golden>11
  130.       \advance \epact by 1
  131.     \fi
  132.   \else
  133.     \ifnum \epact=24
  134.       \advance \epact by 1
  135.     \fi
  136.   \fi
  137. %
  138. % Calculate the phase, using the magic numbers defined above.
  139. % Note that phase may be equal to 8 (== 0) on two days of the year
  140. % due to the way the algorithm was implemented.
  141. %    phase = (((((diy + epact) * 6) + 11) % 177) / 22) & 7;
  142. %
  143.   \x=\diy \advance\x by \epact \multiply\x by 6 \advance\x by 11
  144.   \m=\x \divide\m by 177 \multiply\m by 177 \advance\x by -\m
  145.   \divide\x by 22
  146.   \ifcase\x new\or waxing crescent\or in its first quarter\or
  147.     waxing gibbous\or full\or waning gibbous\or
  148.     in its last quarter\or waning crescent\or new\fi
  149. }
  150.